﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.View == "undefined") Axis.View = {};
if (typeof Axis.View.Member == "undefined") Axis.View.Member = {};
Axis.View.Member.Register = {
    Init: function () {
        var thisObj = this;
        thisObj.Dialog.Init();
        thisObj.Event();
        var fn_yearRange = function () {
            if (typeof _SYS_GLOBAL != "undefined") {
                var currentYear = parseFloat(_SYS_GLOBAL.DATE.YEAR);
                var year_from = currentYear - 70;
                var year_to = currentYear - 18;
                return year_from + ":" + year_to;
            }
            else {
                return 1900 + ":" + 1995;
            }
        };
        var cMaskDate = new CMaskDate({
            yearRange: fn_yearRange()
        });
        cMaskDate.Init("#txtRegisterDateOfBirth");
    },
    Dialog: {
        Instance: null,
        Init: function () {
            var thisObj = this;
            thisObj.Instance = new CDialog({
                title: "• Đăng Ký •",
                width: 600,
                zIndex: 999,
                autoOpen: false,
                draggable: false,
                open: function () {

                },
                beforeClose: function (event, ui) {
                    $(".CValidator-Error").hide();
                }
            });
            thisObj.Instance.Init("#Form-Register");
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Open();
            window.setTimeout(function () {
                $("label[for='rad_RegisterGenderMale']:eq(1)").trigger("click");
                $(".Form-Register").find("#imgCaptchaRefresh").trigger("click");
            }, 1500);
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Close();
        }
    },
    Register: function () {
        var thisObj = this;
        var cvalidator = new CValidator({
            zIndex: 9999
        });
        cvalidator.Init("#Form-Register :input");
        if (cvalidator.CheckValidity()) {
            var pData = {
                pEmail: $("#txtRegisterEmail").val(),
                pFirstName: $("#txtRegisterFirstName").val(),
                pLastName: $("#txtRegisterLastName").val(),
                pPassword: $("#txtRegisterPassword").val(),
                pDateOfBirth: $.CFormat.FormatDate($("#txtRegisterDateOfBirth").val(), "yyyy/MM/dd", "vi-VN"),
                pGender: $("input[name='rad_RegisterGender']:checked").attr("value"),
                pCaptcha: $(".Form-Register").find("#txtCaptchaCode").val()
            };
            var overlay = new COverlay({
                autoShow: false
            });
            overlay.Init();
            $.Ajax({
                url: '../Accounts/Service/AccountService.asmx/MemberRegister',
                type: 'POST',
                data: pData,
                success: function (data, textStatus, jqXHR) {
                    if (data) {
                        data = $.findInXml(data);
                        data = eval("(" + data + ")");
                        if (data.IsSuccessfull) {
                            thisObj.Dialog.Close();
                        }
                        else {

                        }
                        var dialog = new CDialog({
                            dialogClass: "Alert",
                            width: 550,
                            title: "Thông báo",
                            beforeClose: function (event, ui) {
                                $(".CValidator-Error").hide();
                                $(".Form-Register").find("#imgCaptchaRefresh").trigger("click");
                            },
                            buttons: [
                               {
                                   text: "Đóng",
                                   click: function () {
                                       $(this).dialog("close");
                                   }
                               }
                            ]
                        });
                        var html = $.string.Format('<div>{0}</div>', data.Description);
                        overlay.Hide();
                        dialog.Init(html);

                    }
                },
                beforeSend: function (jqXHR, settings) {
                    overlay.Show();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    overlay.Hide();
                },
                complete: function (XMLHttpRequest, textStatus) {

                }
            });
        }
        else {
            return false;
        }
    },
    CheckEmail: function (pEmail) {
        var emailRe = /^([a-z0-9_\.\-\+]+)@([\da-z\.\-]+)\.([a-z\.]{2,6})$/i;
        var cvalidator = new CValidator({
            zIndex: 9999
        });
        cvalidator.Init("#txtRegisterEmail");
        if (!cvalidator.CheckValidity()) {
            return false;
        }

        $.Ajax({
            url: "../Accounts/Service/AccountService.asmx/CheckEmail",
            type: 'POST',
            data: {
                pEmail: pEmail
            },
            beforeSend: function () {
            },
            success: function (data, textStatus, xhr) {
                data = $(data).find("string").text();
                if (data) {
                    data = eval("(" + data + ")");
                    var msg = data.Description;
                    var color = "#000";
                    if (data.IsSuccessfull) {
                        color = "#00CC00";
                    }
                    else {
                        color = "#FF0000";
                    }
                    $(".email-message").html(msg).css("color", color);
                }
            },
            error: function (xhr, textStatus, errorThrown) {

            }
        });
    },
    Event: function () {
        var thisObj = this;
        $("input:radio[name='rad_RegisterGender']").click(function () {
            $("#txt_RegisterGender").val($(this).val());
        });
        $("#btn_RegisterSubmit").click(function () {
            thisObj.Register();
        });
        $("#txtRegisterEmail").focusout(function () {
            var $this = $(this);
            var value = $this.val();
            thisObj.CheckEmail(value);
        });
    }
}
$(function () {
    Axis.View.Member.Register.Init();
});

function ShowDialogRegister() {
    Axis.View.Member.Register.Dialog.Open();
}